package com.shire.shire_interface.common.utils;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.constants.WxConstants;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

public class ChatUtil {
    private static String PROPERTY_CONTENT = "content";
    private static String PROPERTY_TITLE = "title";
    private static String PROPERTY_URL = "url";
    private static String PROPERTY_SRC = "src";
    private static String PROPERTY_WXID = "wxid";
    private static String PROPERTY_MSG = "msg";  //朋友圈文字消息

    public static WxConstants.CmdType getTimelineReqContent(String json,
                                                            JSONObject resultReqObject
    ) {
        JSONObject inputJson = null;
        WxConstants.CmdType cmdType = WxConstants.CmdType.UNKNOWN;
        WxConstants.MessageType type = WxConstants.MessageType.UNKNOWN;
        try {
            inputJson = JSONObject.parseObject(json);
            type = WxConstants.MessageType.get(inputJson.getInteger("msgtype"));
            String fileName = "";
            switch (type) {
                case UNKNOWN:
                    break;
                case TEXT:
                    resultReqObject.put("msg", inputJson.getString(PROPERTY_MSG));
                    cmdType = WxConstants.CmdType.CRK_SEND_TIMELINE_TEXT_D;
                    break;
                case PIC:
                    JSONArray fileNameArray = inputJson.getJSONArray(PROPERTY_SRC);
                    List<String> imageData = new ArrayList<String>();

                    for (Object file : fileNameArray) {
                        imageData.add(Base64.getEncoder().encodeToString(FileUtils.readFileToByteArray(ChatUtil.getFilePath((String) file))));
                    }
                    resultReqObject.put("msg", inputJson.getString(PROPERTY_MSG));
                    resultReqObject.put("imgdata", imageData);
                    resultReqObject.put("imgURL", fileNameArray);
                    cmdType = WxConstants.CmdType.CRK_SEND_TIMELINE_IMAGE_D;
                    break;
                case VIDEO:
                    resultReqObject.put("msg", inputJson.get(PROPERTY_MSG));
                    fileName = ChatUtil.getFilePath((String) inputJson.get(PROPERTY_SRC));
                    resultReqObject.put("videodata", Base64.getEncoder().encodeToString(FileUtils.readFileToByteArray(fileName)));
                    resultReqObject.put("videoURL", inputJson.get(PROPERTY_SRC));
                    cmdType = WxConstants.CmdType.CRK_SEND_TIMELINE_VIDEO_D;
                    break;
                case LINK_CARD:
                    resultReqObject.put("msg", inputJson.getString(PROPERTY_MSG));
                    resultReqObject.put("title", inputJson.getString(PROPERTY_TITLE));
                    resultReqObject.put("desc", inputJson.getString(PROPERTY_CONTENT));
                    resultReqObject.put("linkURL", inputJson.getString(PROPERTY_URL));
                    resultReqObject.put("imgURL", inputJson.getJSONArray(PROPERTY_SRC).get(0));
                    cmdType = WxConstants.CmdType.CRK_SEND_TIMELINE_LINK_D;
                    break;
            }

            if (inputJson.containsKey(PROPERTY_WXID)) {
                resultReqObject.put("blacklistWxIds", inputJson.get(PROPERTY_WXID));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return cmdType;
    }

    public static JSONArray getBatchMsgReqContent(String json) {
        JSONArray inppuArray = JSONArray.parseArray(json);
        JSONArray jsonArray = new JSONArray();
        try {
            for (Object o : inppuArray) {
                JSONObject itemObj = (JSONObject) o;
                WxConstants.MessageType type = WxConstants.MessageType.get(itemObj.getInteger("msgtype"));
                String fileName = "";
                JSONObject reqItem = new JSONObject();
                switch (type) {
                    case UNKNOWN:
                        continue;
                    case PIC:
                        fileName = ChatUtil.getFilePath((String) itemObj.get(PROPERTY_SRC));
                        reqItem.put("imgdata", Base64.getEncoder().encodeToString(FileUtils.readFileToByteArray(fileName)));
                        reqItem.put("type", "image");
                        reqItem.put("imgURL", itemObj.getString(PROPERTY_SRC));
                        break;
                    case TEXT:
                        reqItem.put("msg", itemObj.get(PROPERTY_CONTENT));
                        reqItem.put("at", itemObj.get(PROPERTY_WXID));
                        reqItem.put("type", "text");
                        break;
                    case FRIEND_CARD:
                        reqItem.put("shareWxId", itemObj.get(PROPERTY_WXID));
                        reqItem.put("type", "namecard");
                        break;
                    case FILE:
                        reqItem.put("fileName", itemObj.get(PROPERTY_TITLE));
                        fileName = ChatUtil.getFilePath((String) itemObj.get(PROPERTY_SRC));
                        reqItem.put("fileContentBase64", Base64.getEncoder().encodeToString(FileUtils.readFileToByteArray(fileName)));
                        reqItem.put("type", "file");
                        reqItem.put("fileURL", itemObj.getString(PROPERTY_SRC));
                        break;
                    case VIDEO:
                        fileName = ChatUtil.getFilePath((String) itemObj.get(PROPERTY_SRC));
                        reqItem.put("videoContentBase64", Base64.getEncoder().encodeToString(FileUtils.readFileToByteArray(fileName)));
                        reqItem.put("type", "video");
                        reqItem.put("videoURL", itemObj.getString(PROPERTY_SRC));
                        break;
                    case VOICE:
                        break;
                    case LINK_CARD:
                        reqItem.put("title", itemObj.get(PROPERTY_TITLE));
                        reqItem.put("desc", itemObj.get(PROPERTY_CONTENT));
                        reqItem.put("link", itemObj.get(PROPERTY_URL));
                        reqItem.put("imageURL", itemObj.get(PROPERTY_SRC));
                        reqItem.put("type", "link");
                        break;
                    default:
                        continue;
                }

                jsonArray.add(reqItem);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonArray;
    }

    /***
     * 根据json内容，给resultReqObject添入内容
     * @param json
     * @param resultReqObject
     * @return 消息类型
     */
    public static WxConstants.CmdType getMsgRepContent(String json,
                                                       JSONObject resultReqObject,
                                                       String toUserWxId,
                                                       String toUserAlias,
                                                       String toUserNickName
    ) {
        JSONObject inputJson = null;
        WxConstants.CmdType cmdType = WxConstants.CmdType.UNKNOWN;
        WxConstants.MessageType type = WxConstants.MessageType.UNKNOWN;
        resultReqObject.put("toUserWxId", toUserWxId);
        try {
            inputJson = JSONObject.parseObject(json);
            type = WxConstants.MessageType.get(inputJson.getInteger("msgtype"));
            String fileName = "";
            switch (type) {
                case UNKNOWN:
                    break;
                case PIC:
                    fileName = ChatUtil.getFilePath((String) inputJson.get(PROPERTY_SRC));
                    resultReqObject.put("imgdata", Base64.getEncoder().encodeToString(FileUtils.readFileToByteArray(fileName)));
                    resultReqObject.put("imgURL", inputJson.getString(PROPERTY_SRC));
                    cmdType = WxConstants.CmdType.CRK_SEND_CHAT_D_IMAGE;
                    break;
                case TEXT:
                    resultReqObject.put("msg", inputJson.get(PROPERTY_CONTENT));
                    resultReqObject.put("toUserAlias", toUserAlias);
                    resultReqObject.put("at", inputJson.get(PROPERTY_WXID));
                    cmdType = WxConstants.CmdType.CRK_SEND_CHAT_D;
                    break;
                case FRIEND_CARD:
                    resultReqObject.put("shareWxId", inputJson.get(PROPERTY_WXID));
                    cmdType = WxConstants.CmdType.CRK_SEND_CHAT_D_NAMECARD;
                    break;
                case FILE:
                    resultReqObject.put("fileName", inputJson.get(PROPERTY_TITLE));
                    fileName = ChatUtil.getFilePath((String) inputJson.get(PROPERTY_SRC));
                    resultReqObject.put("fileContentBase64", Base64.getEncoder().encodeToString(FileUtils.readFileToByteArray(fileName)));
                    resultReqObject.put("fileURL", inputJson.getString(PROPERTY_SRC));
                    cmdType = WxConstants.CmdType.CRK_SEND_CHAT_D_FILE;
                    break;
                case VIDEO:
                    fileName = ChatUtil.getFilePath((String) inputJson.get(PROPERTY_SRC));
                    resultReqObject.put("videoContentBase64", Base64.getEncoder().encodeToString(FileUtils.readFileToByteArray(fileName)));
                    resultReqObject.put("videoURL", inputJson.getString(PROPERTY_SRC));
                    cmdType = WxConstants.CmdType.CRK_SEND_CHAT_D_VIDEO;
                    break;
                case VOICE:
                    break;
                case LINK_CARD:
                    resultReqObject.put("title", inputJson.get(PROPERTY_TITLE));
                    resultReqObject.put("desc", inputJson.get(PROPERTY_CONTENT));
                    resultReqObject.put("link", inputJson.get(PROPERTY_URL));
                    resultReqObject.put("imageURL", inputJson.get(PROPERTY_SRC));
                    cmdType = WxConstants.CmdType.CRK_SEND_CHAT_D_LINK;
                    break;
            }
        } catch (Exception e) {
            resultReqObject.put("msg", json);
            cmdType = WxConstants.CmdType.CRK_SEND_CHAT_D;
            resultReqObject.put("toUserNick", toUserNickName);
            resultReqObject.put("toUserAlias", toUserAlias);
        }

        return cmdType;
    }

    public static String getFilePath(String fileName) {
        File path = new File("");
        try {
            path = new File(ResourceUtils.getURL("classpath:").getPath());
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!path.exists()) {
            path = new File("");
        }

        String dirPath = "static" + File.separator + fileName;
        return path.getAbsolutePath() + File.separator + dirPath;
    }
}
